//
//  ZLImpressViewController.m
//  TripTogether
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 杨泓晔. All rights reserved.
//

#import "ZLImpressViewController.h"
#import "ZLTravelsModel.h"
#import "ZLImpressTableViewCell.h"
#import "WJQNetwork.h"
#import "SizeForLabel.h"
#import "ZLSecondTableViewCell.h"
#import "ZLDetailImageModel.h"
#import "ZLFooterModel.h"
#import "StarView.h"
#import "ZLButtonTableViewCell.h"
#import "ZLTravelsViewController.h"
#import "ZLDescriptionViewController.h"
#import "ZLMapViewController.h"
#import "ZLFirstViewController.h"


@interface ZLImpressViewController ()<UITableViewDataSource,UITableViewDelegate,ZLButtonTableViewCellDelegate>

@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)NSMutableArray *myArray;
@property (nonatomic, strong)NSMutableArray *secondArray;
@property (nonatomic, strong)ZLDetailImageModel *zlDetailImageM;

@property (nonatomic, strong)UIImageView *myImageView;
@property (nonatomic, strong)StarView *starV;

@property (nonatomic, strong)UILabel *recommended_reasonLabel;
@property (nonatomic, strong)UILabel *rating_usersLabel;

@property (nonatomic, strong)UIView *navCView;
@end

@implementation ZLImpressViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.myArray = [NSMutableArray array];
    _secondArray = [NSMutableArray array];
    //http://api.breadtrip.com/destination/place/2/6636/pois/all/?sort=default&start=0&latitude=38.883499&longitude=121.544943&sign=a4661007f088a14d6dcd37eddd67b078
    //http://api.breadtrip.com/destination/place/5/2388329328/
    [self createTableView];
    [self getData];
    [self getSecondData];
    [self MBShow];
    
}
#pragma mark - 创建TableView
- (void)createTableView
{
    self.myTableView = [[UITableView alloc]initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    self.myTableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
    //注册
    [self.myTableView registerClass:[ZLImpressTableViewCell class] forCellReuseIdentifier:@"ZLImpressTableViewCell"];
    [self.myTableView registerClass:[ZLSecondTableViewCell class] forCellReuseIdentifier:@"ZLSecondTableViewCell"];
    [self.myTableView registerClass:[ZLButtonTableViewCell class] forCellReuseIdentifier:@"ZLButtonTableViewCell"];
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //****************
    self.myTableView.contentInset = UIEdgeInsetsMake(500* AUTO_HEIGHT, 0, 0, 0);
    [self createHeaderImage];
    
}



#pragma mark - tableView协议
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.myArray.count;
    }else if(section == 1){
        return 1;
    }else{
        return _secondArray.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *cellInd = @"ZLImpressTableViewCell";
        ZLImpressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInd];
        ZLTravelsModel *bigM = [self.myArray objectAtIndex:indexPath.row];
            cell.bigM = bigM;
        cell.cellArray = [NSMutableArray arrayWithArray:[[self.myArray objectAtIndex:indexPath.row] waypoints]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if(indexPath.section == 2){
        ZLSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZLSecondTableViewCell"];
                cell.zlDetailImageM = self.zlDetailImageM;
        
        ZLFooterModel *mod = _secondArray[indexPath.row];
        cell.myLabel.text = mod.title;
        cell.nameLabel.text = mod.value;
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ([mod.title isEqualToString:@"概况"] || [mod.title isEqualToString:@"地址"]) {
            
        } else {
            cell.userInteractionEnabled = NO;
            
        }
        
        return cell;
    }else{
        ZLButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZLButtonTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.buttonTableViewCellDelegate = self;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ZLTravelsModel *bigM = [self.myArray objectAtIndex:indexPath.row];
        return 245*AUTO_HEIGHT + bigM.height;
    }else if(indexPath.section == 1){
        
        return 140*AUTO_HEIGHT;
        
    }else{
        if (self.zlDetailImageM.descriptionss.length > 0) {
            return 60 + 30;
        }else{
            return 30 + 30;
        }
    }
}
#pragma mark - 点击触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZLFooterModel *mod = _secondArray[indexPath.row];
    if (indexPath.section == 0) {
        
        
        
    }else if(indexPath.section == 2){
       
        if ([mod.title isEqualToString:@"概况"] || [mod.title isEqualToString:@"地址"]) {
            if (indexPath.row == 0) {
                ZLDescriptionViewController *descriptionVC = [ZLDescriptionViewController new];
                descriptionVC.name = self.name;
                descriptionVC.hehe = mod.value;
                
                [self.navigationController pushViewController:descriptionVC animated:YES];
                
            }else if(indexPath.row == 1){
                ZLMapViewController *mapVC = [ZLMapViewController new];
                mapVC.name = self.name;
                mapVC.myId = self.myId;
                mapVC.type = self.type;
                mapVC.lat = self.lat;
                mapVC.lng = self.lng;
                [self.navigationController pushViewController:mapVC animated:YES];
            }
        }
        
        
    }
    
    
    
}

#pragma mark - 下拉图片创建
- (void)createHeaderImage
{
    self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -500*AUTO_HEIGHT, SCREEN_WIDTH, 240*AUTO_HEIGHT)];
    [self.myTableView addSubview:self.myImageView];
    self.myImageView.userInteractionEnabled = YES;
    self.myImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.myImageView sd_setImageWithURL:[NSURL URLWithString:self.headerImage] placeholderImage:PICHOLDEER];
    
    self.navCView = [[UIView alloc]init];
    [self.view addSubview:self.navCView];
    self.navCView.backgroundColor = [UIColor colorWithRed:0.33 green:0.74 blue:0.8 alpha:1];
    self.navCView.alpha = 0;
    [self.navCView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.equalTo(self.view);
        make.height.equalTo(@64);
    }];
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setImage:[UIImage imageNamed:@"icon_nav_back_button@2x.png"] forState:(UIControlStateNormal)];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(10);
        make.top.equalTo(self.navCView.mas_top).offset(15);
        make.height.equalTo(@49);
        make.width.equalTo(@30);
    }];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
//*********
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, -260*AUTO_HEIGHT, SCREEN_WIDTH, 260*AUTO_HEIGHT)];
    [self.myTableView addSubview:myView];

    myView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * AUTO_HEIGHT, SCREEN_WIDTH, 50)];
    [myView addSubview:nameLabel];
    nameLabel.textAlignment = NSTextAlignmentCenter;

    nameLabel.font = [UIFont fontWithName:@"Rounded MT Bold" size:28.0f];
    nameLabel.text = self.name;
    nameLabel.font = [UIFont systemFontOfSize:29.0f];
    //星星
    _starV = [[StarView alloc]initWithFrame:CGRectMake(80*AUTO_WIDTH, 100*AUTO_HEIGHT, 30*AUTO_WIDTH, 20*AUTO_HEIGHT) number:5];
    [myView addSubview:_starV];
    
//*****下拉图片下方
    self.rating_usersLabel = [[UILabel alloc]initWithFrame:CGRectMake(220*AUTO_WIDTH, 100*AUTO_HEIGHT, 100*AUTO_WIDTH, 20*AUTO_HEIGHT)];
    [myView addSubview:self.rating_usersLabel];
    
    
    self.recommended_reasonLabel = [[UILabel alloc]initWithFrame:CGRectMake(20*AUTO_WIDTH, 140*AUTO_HEIGHT, 335*AUTO_WIDTH, 90*AUTO_HEIGHT)];
    [myView addSubview:self.recommended_reasonLabel];
    self.recommended_reasonLabel.textAlignment = NSTextAlignmentCenter;
    self.recommended_reasonLabel.numberOfLines = 4;
    self.recommended_reasonLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
    //面包印象
    UILabel *impressLabel = [UILabel new];
    impressLabel.text = @"印象";
    impressLabel.textAlignment = NSTextAlignmentCenter;
    [myView addSubview:impressLabel];
    [impressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(myView.mas_left).offset(120*AUTO_WIDTH);
        make.right.equalTo(myView.mas_right).offset(-120*AUTO_WIDTH);
        make.top.equalTo(self.recommended_reasonLabel.mas_bottom).offset(5*AUTO_HEIGHT);
        make.height.equalTo(@(20*AUTO_HEIGHT));
    }];
    
    UIView *lineOneV = [[UIView alloc]init];
    [myView addSubview:lineOneV];
    lineOneV.backgroundColor = [UIColor lightGrayColor];
    [lineOneV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(myView.mas_left).offset(20*AUTO_WIDTH);
        make.right.equalTo(impressLabel.mas_left);
        make.top.equalTo(self.recommended_reasonLabel.mas_bottom).offset(15*AUTO_HEIGHT);
        make.height.equalTo(@(1*AUTO_HEIGHT));
    }];
    
    UIView *lineTwoV = [[UIView alloc]init];
    [myView addSubview:lineTwoV];
    lineTwoV.backgroundColor = [UIColor lightGrayColor];
    [lineTwoV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(myView.mas_right).offset(-20*AUTO_WIDTH);
        make.left.equalTo(impressLabel.mas_right);
        make.top.equalTo(self.recommended_reasonLabel.mas_bottom).offset(15*AUTO_HEIGHT);
        make.height.equalTo(@(1*AUTO_HEIGHT));
    }];
    
    
}

#pragma mark - 返回按钮
- (void)btnAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 下拉放大方法实现
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-500
    self.myImageView.layer.masksToBounds = YES;
    
    
    CGFloat y = self.myTableView.contentOffset.y;

        self.navCView.alpha = 1.0 - (-y/500);
    
        if (y < - 500*AUTO_HEIGHT) {
        CGRect frame = self.myTableView.frame;
        frame.origin.y = y;//偏移了多少
        frame.size.height = -y;
        self.myImageView.frame = frame;
    }
}


#pragma mark - 请求数据
- (void)getData
{
    NSString *string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/poi_trips/?start=0&count=20",self.type,self.myId];
    [WJQNetwork getAFNet:string block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableArray *arr = [dic objectForKey:@"trips"];
        for (NSMutableDictionary *Dic in arr) {
            ZLTravelsModel *bigM = [ZLTravelsModel new];
            [bigM setValuesForKeysWithDictionary:Dic];
#pragma mark - 自适应trip_text的高度
            CGSize digestHeight = [SizeForLabel labelRectWithSize:CGSizeMake(SCREEN_WIDTH - 20*AUTO_WIDTH, 2000*AUTO_HEIGHT) LabelText:bigM.trip_text Font:[UIFont systemFontOfSize:16.0f]];
            if (bigM.trip_text.length == 0) {
                bigM.height = 0;
            }else{
                bigM.height = digestHeight.height;
            }
            if (self.myArray.count < 3) {
               [self.myArray addObject:bigM];
            }else{
                continue;
            }
            
        }
        //        NSLog(@"%@",[[self.myArray objectAtIndex:1] trip_text]);
        //        NSLog(@"%@",[[[self.myArray objectAtIndex:1] myTrip] name]);
        //
        //        NSLog(@"%@",[[[[self.myArray objectAtIndex:1]waypoints] objectAtIndex:0] photo]);
        
        //        NSLog(@"%ld",[[[[[[self.myArray objectAtIndex:1] myTrip] fuckM] hehe] papa] value]);
        //        NSLog(@"%ld",(long)[[[[[self.myArray objectAtIndex:1] waypoints] objectAtIndex:0] littleM] w]);
        [self.myTableView reloadData];

        
    } errorBlock:^(NSError *error) {
        
        
    }];

}


- (void)getSecondData
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.type,self.myId];
    [WJQNetwork getAFNet:str block:^(id result) {
        [_secondArray removeAllObjects];
        
        NSMutableDictionary *dic = result;
        self.zlDetailImageM = [ZLDetailImageModel new];
        [_zlDetailImageM setValuesForKeysWithDictionary:dic];
        [_starV setStarWithNumber:_zlDetailImageM.rating];
        _rating_usersLabel.text = [NSString stringWithFormat:@"%ld人点评",_zlDetailImageM.rating_users];
        self.recommended_reasonLabel.text = _zlDetailImageM.recommended_reason;
        
        if (0 != _zlDetailImageM.descriptionss.length) {
            ZLFooterModel *footerMod = [ZLFooterModel new];
            footerMod.title = @"概况";
            footerMod.value = _zlDetailImageM.descriptionss;
            [_secondArray addObject:footerMod];
        }

        if (0 != _zlDetailImageM.address.length) {
            
            ZLFooterModel *footerMod = [ZLFooterModel new];
            footerMod.title = @"地址";
            footerMod.value = _zlDetailImageM.address;
            
            [_secondArray addObject:footerMod];
        }
        if (0 != _zlDetailImageM.arrival_type.length) {
            
            ZLFooterModel *footerMod = [ZLFooterModel new];
            footerMod.title = @"到达方式";
            footerMod.value = _zlDetailImageM.arrival_type;
            
            [_secondArray addObject:footerMod];
        }
        if (0 != _zlDetailImageM.opening_time.length) {
            
            ZLFooterModel *footerMod = [ZLFooterModel new];
            footerMod.title = @"开放时间";
            footerMod.value = _zlDetailImageM.opening_time;
            
            [_secondArray addObject:footerMod];
        }
        if (0 != _zlDetailImageM.time_consuming.length) {
            
            ZLFooterModel *footerMod = [ZLFooterModel new];
            footerMod.title = @"浏览耗时";
            footerMod.value = _zlDetailImageM.time_consuming;
            
            [_secondArray addObject:footerMod];
        }
        [self MBHide];
        [self.myTableView reloadData];
    } errorBlock:^(NSError *error) {
        
        
    }];
}


#pragma mark - 按钮 更多印象 协议方法实现
- (void)toMoreImpress
{
    ZLTravelsViewController *travelsVC = [ZLTravelsViewController new];
    travelsVC.type = self.type;
    travelsVC.myId = self.myId;
    [self.navigationController pushViewController:travelsVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
